<template>
  <div class="search-history">
    <van-cell title="搜索历史">
      <div v-if="isDeleteShow">
        <span  @click="$emit('clear-search-histories')">全部删除</span>&nbsp;&nbsp;&nbsp;&nbsp;
        <span @click="isDeleteShow = false">完成</span>
      </div>
      <van-icon v-else name="delete" @click="isDeleteShow = true" />
    </van-cell>
    <van-cell
      v-for="(item,index) in histories"
      :key="index"
      :title="item"
      @click="onSearchItemClick(item, index)"
      >
        <van-icon
            name="close"
            v-if="isDeleteShow"
        />
    </van-cell>

  </div>
</template>

<script>
import { setItem } from '@/utils/storage'
export default {
  name: 'searchHistory',
  props: {
    histories: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      isDeleteShow: false
    }
  },
  created () {
    console.log(this.histories)
  },
  mounted () {

  },
  methods: {
    onSearchItemClick (item, index) {
      console.log(index)
      if (this.isDeleteShow) {
        this.histories.splice(index, 1)
        // console.log(this.histories)
        setItem('history', this.histories)
      } else {
        this.$emit('search', item)
      }
    }

  }
}
</script>

<style scoped lang="less">

</style>
